﻿using XYAC.Framework.MonitorTireMaterialManual.Entities;

namespace XYAC.Framework.MonitorTireMaterialManual.DataExporter
{
    // Class representing the entire data file
    public class DataFile
    {
        public float FileInertvaWidth { get; set; }
        public double XReference { get; set; }
        public List<Point> Points { get; set; }
        public List<MeasureKey> MeasureKeys { get; set; }
        public string TypeName { get; set; }
        public int ID { get; set; }
        public List<StableKeys> StableKeys { get; set; }
        public float MeasureLeftArea { get; set; }
        public float MeasureRightArea { get; set; }
        /// <summary>
        /// 测量面积=左侧测量面积+右侧测量面积
        /// </summary>
        public float MeasureArea { get; set; }
        /// <summary>
        /// 标准面积=左侧标准面积+右侧标准面积
        /// </summary>
        public float Area { get; set; }
        public float LeftArea { get; set; }
        public float RightArea { get; set; }
        //public ClsMyNominal SpecStable { get; private set; }
        /// <summary>
        /// 胎侧：TXT文档，[Keys]（总行数/2）+1行的左边数字
        /// 胎侧左宽度
        /// </summary>
        public float SideLeftWidth { get; set; }
        /// <summary>
        /// 胎侧：TXT文档，[Keys]（总行数/2）+1行的左边数字
        /// 胎侧右宽度
        /// </summary>
        public float SideRightWidth { get; set; }

        /// <summary>
        /// 胎侧：TXT文档，TXT文档，[MeasureKeys]最后一行左边数字-[XReference]
        /// 胎侧右宽度标准值
        /// </summary>
        public float SideRightWidthInfact { get; set; }

        /// <summary>
        /// 胎侧：TXT文档，[XReference]的数值
        /// 胎侧左宽度,它实际上=XReference
        /// 
        /// </summary>
        public float SideLeftWidthInfact { get; set; }



        private static List<Point> ConvertToPoints(ClsMyMeasuring myMeasuring)
        {
            var points = new List<Point>();
            int count = Math.Min(myMeasuring.ThicknessPoints.Count, myMeasuring.BottomHeightPoints.Count);

            for (int i = 0; i < count; i++)
            {
                var point = new Point
                {
                    Index = i,
                    X = myMeasuring.ThicknessPoints[i].Point.Y,
                    Y = myMeasuring.BottomHeightPoints[i].Point.Y
                };
                points.Add(point);
            }

            return points;
        }
        public DataFile()
        {
            MeasureKeys = new List<MeasureKey>();
            StableKeys = new List<StableKeys>();
            Points=new List<Point>();
        }
        /// <summary>
        /// 验证 DataFile 对象的完整性和有效性。
        /// </summary>
        /// <param name="errors">包含所有验证错误信息的列表。</param>
        /// <returns>如果验证成功，则返回 true；否则返回 false。</returns>
        public bool Validate(out List<string> errors)
        {
            errors = new List<string>();

            // 验证集合是否为空 

            if (MeasureKeys == null || MeasureKeys.Count == 0)
                errors.Add("MeasureKeys 集合不能为空且必须包含至少一个元素。");

            if (StableKeys == null || StableKeys.Count == 0)
                errors.Add("Keys 集合不能为空且必须包含至少一个元素。");

            // 验证字符串属性
            if (string.IsNullOrWhiteSpace(TypeName))
                errors.Add("TypeName 不可为空或空字符串。");

            // 验证 ID
            if (ID == 0)
                errors.Add("ID 不可为零。");

            // 其他可选的验证规则
            // 例如，验证 FileInertvaWidth 是否在合理范围内
            // if (FileInertvaWidth < 0 || FileInertvaWidth > 1)
            //     errors.Add("FileInertvaWidth 必须在 0 和 1 之间。");

            // 返回是否通过所有验证
            return errors.Count == 0;
        }
    }

    // Class representing a point in the [Points] section
    public class Point
    {
        public int Index { get; set; }
        public float X { get; set; }
        public float Y { get; set; }
    }

    // Class representing a measure key in the [MeasureKeys] section
    public class MeasureKey
    {
        public int Index { get; set; } = 0;
        public double Coordinate { get; set; }
        public double Value { get; set; }
    }
    // Class representing a key in the [Keys] section
    public class StableKeys
    {
        public double Coordinate { get; set; }
        public double Value { get; set; }
    }

}
